package edu.hawaii.its381b.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import java.lang.Math.*;

public class HealthBar {
	
	private ImageElement healthBarImage, blackBarImage;
	private int drawAtX, drawAtY, barWidth, barHeight,
				currentHealth;
	private final int maxHealth;
	private Canvas healthBarCanvas;
//	private double percentHealth;
	private int barHealthLeft;
	
	public HealthBar(int canvasX, int canvasY, int gameBarWidth, int gameBarHeight,
					 int gameMaxHealth, Canvas gameHealthBarCCanvas){
		drawAtX = canvasX;
		drawAtY = canvasY;
		barWidth= gameBarWidth;
		barHeight= gameBarHeight;
		healthBarCanvas = gameHealthBarCCanvas;
		maxHealth = gameMaxHealth;
		
    	SafeUri healthBarURI = HermitCrabResources.INSTANCE.healthBar().getSafeUri();
    	healthBarImage =  ImageElement.as((new Image(healthBarURI )).getElement());
    	
    	SafeUri blackBarURI = HermitCrabResources.INSTANCE.blackBar().getSafeUri();
    	blackBarImage =  ImageElement.as((new Image(blackBarURI )).getElement());
	}
	
	public void setHealth(int currentHealth){
	/*	int percentHealth = currentHealth/(maxHealth*100);
		int amountOfBarToErase = barWidth * (1 - (percentHealth/100));
		if(amountOfBarToErase  >  barWidth ){
			amountOfBarToErase = barWidth;
			
		String s = "0.01";
		double d = Double.parseDouble(s);
		int i = (int) d; 
		}*/
		/* percentHealth = (currentHealth/barWidth) *  maxHealth;
		barHealthLeft = Math.round((currentHealth/maxHealth) * barWidth); */
		double percentHealth, barWidthDec = barWidth, currentHealthDec = currentHealth, maxHealthDec = maxHealth;
		
		percentHealth = (currentHealthDec/maxHealthDec) *  barWidthDec;
		barHealthLeft = (int) percentHealth;
		
		/* RootPanel.get().add(new Label("barHealthLeft: "+barHealthLeft+
		 *
									  " drawAtX: "+drawAtX+
									  " percentHealth: "+percentHealth+
									  " maxHealth: "+maxHealth+
									  " currentHealth: "+currentHealth+
									  "barWidth: "+barWidth)); */

		healthBarCanvas.getContext2d().drawImage(healthBarImage, drawAtX, drawAtY, barWidth, barHeight);
		healthBarCanvas.getContext2d().drawImage(blackBarImage, drawAtX + 2 + barHealthLeft, drawAtY + 2, 
				barWidth - barHealthLeft - 4, barHeight -6);
		
	}
}
